<?php
/**
 * 用户中心
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/10
 * Time: 14:50
 */

namespace app\api\controller;

use app\api\business\UserBus;
use think\Exception;
use think\facade\Db;
use think\facade\Request;

class User extends UserCommon
{

    public function __construct(){
        parent::__construct();
        $this->url =  Request::scheme().'://' . Request::host();
    }

    /**
     * 获取用户信息
     * @return \think\response\Json
     */
    public function userInfo(){
        $data = (new UserBus())->getInfo();
        return resJson(200,'ok',$data);
    }

    /**
     * 修改信息
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function update()
    {
        $param = input('param.','','trim');
        $res = Db::name('user')->where('user_id', $this->user_id)->update($param);
        if ($res){
            return resJson(200,'修改成功');
        }else{
            return resJson(0,'修改失败');
        }
    }


    /**
     * 绑定手机号码
     * @return \think\response\Json
     */ 
    public function bindMobile()
    {
        try{
            $mobile = input('param.mobile', '', 'trim');
            $check = Db::name('user')->where('user_id', '<>', $this->user_id)->where('mobile', '=', $mobile)->find();
            if ($check) {
                return resJson(0,'该手机号码已被绑定其他账号');
            }
            $sm_code = input('param.sm_code', '', 'trim');
            if (empty($mobile) || empty($sm_code)){
                return resJson(0,'缺失参数');
            };
            $redisCode = cache('bind.'.$mobile);
            if ($redisCode !== $sm_code) {
                throw new Exception('验证码错误',0);
            }
            $res = Db::name('user')->where('user_id',$this->user_id)->update(['mobile' => $mobile]);
            if ($res){
                cache('bind.'.$mobile,NULL);
                return resJson(200,'绑定成功');
            }else{
                return resJson(200,'绑定失败');
            }
        }catch (\Exception $e){
            return resJson($e->getCode(),$e->getMessage());
        }
    }


    /**
     * 签到
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sign()
    {
        $check = Db::name('sign')
            ->where('user_id', '=', $this->user_id)
            ->where('create_time', '>=', date('Y-m-d'), time())
            ->find();
        if ($check) {
            return resJson(0,'今日已签到过');
        }
        $data = [
            'user_id' => $this->user_id,
            'integral' => 1,
            'create_time' => toDate(time())
        ];
        Db::name('sign')->insert($data);
        Db::name('user')->where('user_id', $this->user_id)->inc('integral')->update();
        return resJson(200,'签到成功');
    }

    public function getSignCount()
    {
        $count = Db::name('sign')->where('user_id', $this->user_id)->count();
        return resJson(200,'ok',['count' => $count]);
    }

    /**
     * 检查当日是否签到
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkSign()
    {
        $check = Db::name('sign')
            ->where('user_id', '=', $this->user_id)
            ->where('create_time', '>=', date('Y-m-d'), time())
            ->find();
        $is_sign = 0;
        if ($check) {
            $is_sign = 1;
        }
        return resJson(200, 'ok', ['is_sign' => $is_sign]);
    }

    /**
     * 签到周榜
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function weekly()
    {
        $firstDay = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
        $list = Db::name('sign')->alias('a')
            ->join('user b', 'b.user_id = a.user_id')
            ->field('a.user_id,sum(a.integral) as integral_count,b.username,b.integral,b.photo')
            ->order('integral_count', 'desc')
            ->where('a.create_time','>=',$firstDay)
            ->group('a.user_id')
            ->limit(5)
            ->select()->toArray();
        return resJson(200, 'ok',$list);
    }


    /**
     * 签到月榜
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function monthly()
    {
        $firstDay = date('Y-m-01', time());
        $list = Db::name('sign')->alias('a')
            ->join('user b', 'b.user_id = a.user_id')
            ->field('a.user_id,sum(a.integral) as integral_count,b.username,b.integral,b.photo')
            ->order('integral_count', 'desc')
            ->where('a.create_time','>=',$firstDay)
            ->group('a.user_id')
            ->limit(5)
            ->select()->toArray();
        return resJson(200, 'ok',$list);
    }


    /**
     * 信息反馈
     */
    public function feedback()
    {
        $param = input('param.','','trim');
//        if (isset($param['images'])) {
//            $param['images'] = json_encode($param['images'], true);
//        }
        $param['user_id'] = $this->user_id;
        $param['create_time'] = toDate(time());
        Db::name('feedback')->insert($param);
        return resJson(200, '反馈成功');
    }


    /**
     * 添加观看直播记录
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addBrowseLog()
    {
        $param = input('param.','','trim');
        try{
            (new UserBus())->addBrowseLog($param);
            return resJson(200, 'ok');
        }catch (\Exception $e){
            return resJson(0, $e->getMessage());
        }
    }


    /**
     * 清空观看直播记录
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function clearBrowseLog()
    {
        Db::name('browse_log')->where('user_id', $this->user_id)->delete();
        return resJson(200, '清空成功');
    }


    /**
     * 获取观看记录
     * @return \think\response\Json
     */
    public function getBrowseLog(){
        $type = input('param.type',0,'intval');
        try{
            $list = (new UserBus())->getBrowseLog($type);
            return resJson(200, 'ok',$list);
        }catch (\Exception $e){
            return resJson(0, $e->getMessage());
        }
    }


    /**
     * 交易记录
     * @return \think\response\Json
     */
    public function moneyLog(){
        $type = input('param.type',0,'intval');
        try{
            $list = (new UserBus())->moneyLog($type);
            return resJson(200, 'ok',$list);
        }catch (\Exception $e){
            return resJson(0, $e->getMessage());
        }
    }

}